<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>layDate快速使用</title>
  <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
  <link rel="stylesheet" href="../../css/public.css" media="all">
</head>

<body>
  <div class="layuimini-container">
    <div class="layuimini-main">

      <!-- <fieldset class="table-search-fieldset"> -->
      <div style="margin: 10px 10px 10px 10px">
        <div class="layui-form layui-form-pane" action="">
          <div class="layui-form-item">
            <div class="layui-form-item">
              <label class="layui-form-label">用户名</label>
              <div class="layui-input-block">
                <input type="text" name="username" autocomplete="off" class="layui-input" lay-verify="required">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">密码</label>
              <div class="layui-input-block">
                <input type="password" name="password" autocomplete="off" class="layui-input" lay-verify="pass">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">真实姓名</label>
              <div class="layui-input-block">
                <input type="text" name="reader_name" autocomplete="off" class="layui-input" lay-verify="required">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">邮箱</label>
              <div class="layui-input-block">
                <input type="text" name="email" autocomplete="off" class="layui-input" lay-verify="email">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">用户身份</label>
              <div class="layui-input-block">
                <select id="reader_type" name="reader_type" lay-verify="required">
                  <option value="">请选择</option>
                  <option value="学生">学生</option>
                  <option value="教师">教师</option>
                </select>
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">用户权限</label>
              <div class="layui-input-block">
                <select name="privilege" lay-verify="required">
                  <option value="">请选择</option>
                  <option value="普通用户">普通用户</option>
                  <option value="管理员">管理员</option>
                </select>
              </div>
            </div>
            <!-- <div class="layui-inline">
              <div class="layui-input-inline">
                <button class="layui-form-label" lay-submit lay-filter="saveBtn">确认修改</button>
              </div>
            </div> -->
            <div style="position: absolute; right: 17px;">
              <button id="closebtn" type="button" class="layui-btn" style="margin-right: 0;" lay-submit
                lay-filter="saveBtn">完成添加</button>
            </div>
          </div>
        </div>
      </div>
      <!-- </fieldset> -->
    </div>
  </div>

  <!-- <div style="position: absolute; right: 17px;">
        <button id="closebtn" type="button" class="layui-btn" style="margin-right: 0;">关闭</button>
    </div> -->
  <script src="../../lib/layui-v2.6.3/layui.js"></script>
  <script>
    // var allData = {
    //   "book_id": 0,
    //   "book_name": "",
    //   "publication_data": "",
    //   "publisher": "",
    //   "author": "",
    //   "isbn": "",
    //   "copy_num": 0
    // }
    layui.use(['laydate', 'form', 'jquery', 'table'], function () {
      var laydate = layui.laydate,
        form = layui.form,
        table = layui.table,
        $ = layui.$;
      form.verify({
        pass: [
          /^[\S]{6,12}$/
          , '密码必须6到12位，且不能出现空格'
        ]
      });
      //执行一个laydate实例
      laydate.render({
        elem: '#publication_data' //指定元素
      });

      //监听提交
      form.on('submit(saveBtn)', function (data) {
        // document.getElementById("userPassword").select();
        console.log('添加用户，发送数据如下')
        console.log(JSON.stringify(data.field))
        // tmp = {
        //   table: "book",
        //   method: "add",
        //   username: "userabc",
        //   password: "password", // 以后会改成加密后 TOKEN
        //   data: data.field
        // }
        // console.log(tmp)
        // console.log(JSON.stringify(tmp))
        $.ajax({
          url: "/api2/admin/user/create",
          type: "POST",
          data: JSON.stringify(data.field),
          contentType: 'application/json',
          // data: tmp,
          success: function (data, textStatus) {
            console.log(data)
            console.log(textStatus)
            if (data.code == 0) {
              layer.msg('添加成功', { time: 0.5 * 1000 }, function () {
                // 关闭弹出层
                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
              });
              return true;
            }
            else {
              layer.msg('添加失败，原因：' + data.msg);
              return false;
            }
          },
          error: function (xhr, errorText, errorType) {
            console.log(xhr)
            console.log(xhr.status)
            console.log(errorType)
            layer.msg('添加失败（与服务器连接失败）');
          },
          complete: function () {
            console.log('---------')
          },
          dataType: "json"
        })
        return false;
      });
    });
  </script>
</body>

</html>